import 'package:enjoy_plus_three/pages/house/house_list.dart';
import 'package:flutter/material.dart';
import 'package:enjoy_plus_three/pages/login/index.dart';
import 'package:enjoy_plus_three/pages/notice/index.dart';
import 'package:enjoy_plus_three/pages/profile/index.dart';
import 'package:enjoy_plus_three/pages/tab_bar_page.dart';
import 'package:enjoy_plus_three/utils/token.dart';

void main() {
  runApp(MaterialApp(
    // home: TabbarPage(),
    routes: {
      '/': (context) => const TabbarPage(),
      '/detail': (context) => const NoticeDetail(),
      '/login': (context) => const LoginPage(),
    },
    onGenerateRoute: (settings) {
      final tokenManager = TokenManager();
      final token = tokenManager.getToken() ?? '';
      // settings.name 访问的路径
      if (token.isEmpty && settings.name != '/login') {
        return MaterialPageRoute(builder: (context) => const LoginPage());
      }
      // 执行到这里的时候. token是存在的
      if (settings.name == '/profile') {
        return MaterialPageRoute(
            builder: (context) =>
                ProfilePage(userInfo: settings.arguments as Map));
      }

      if (settings.name == '/house_list') {
        return MaterialPageRoute(builder: (context) => const HouseList());
      }
      if (settings.name == '/location_list') {
        //return MaterialPageRoute(builder: (context) => const LocationList());
      }
      // 统一处理(哪也不让跳) -> -> 实际应跳转到404页面
      return null;
    },
    initialRoute: '/', // 初始化的加载路由的路径
  ));
}
